/**
 * 链表中到数第k个节点
 * @param head 
 * @param k 
 */
const getKthFromEnd = (head: ListNode | null, k: number): ListNode | null => {
    // 边界判断
    if (head === null) return null

    // 使用栈存储 
    const arr: ListNode[] = []
    
    while (head !== null) {
        arr.push(head);
        head = head.next
    }

    // 取出该值
    return arr[arr.length - k]
};

// 寻找擦汗高难度
const getKthFromEnd2 = (head: ListNode | null, k: number): ListNode | null => {
    // 边界判断
    if (head === null) return null

    // 获取链表的长度
    let h = head.next,
        len = 1;
    while (h !== null) {
        len++
        h = h.next;
    }

    // 所找的长度大于链表的长度, 返回空
    len = len - k;

    while (len--) {
        head = head!.next;
    }

    return head;

};

export {}